/**
 * 
 */
package org.openmobo.playn.poc1.core;

import static playn.core.PlayN.graphics;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import playn.core.ImageLayer;

/**
 * @author leonardo.dinnouti@gmail.com
 *
 */
class Ball {
	public ImageLayer layer;
	float angle = 0.0f;
	public Body body;

	public Ball(World world, Image image, float x, float y) {
		float radius = 0.5f;
		layer = graphics().createImageLayer(image);
		layer.setOrigin(image.width() / 2f, image.height() / 2f);
		layer.setScale((radius * 2) / image.width(),
				(radius * 2) / image.height());
		layer.setTranslation(x, y);

		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DYNAMIC;
		bodyDef.position = new Vec2(x, y);
		body = world.createBody(bodyDef);
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = new CircleShape();
		fixtureDef.shape.m_radius = radius;
		fixtureDef.density = 1.0f;
		fixtureDef.friction = 0.3f;
		body.createFixture(fixtureDef);
		body.setTransform(new Vec2(x, y), angle);
	}

	public void paint(float delta) {
		layer.setTranslation(body.getPosition().x, body.getPosition().y);
		angle += delta * 0.1f;
		layer.setRotation(angle);
//		log.info("x="+body.getPosition().x+";y="+body.getPosition().y);
	}
}
